using System;
using System.Collections.Generic;
using offline2048;
using com.adjust.sdk;
using UnityEngine;
using UnityEngine.Purchasing;

/// <summary>
/// 礼包类型，与配置表iap_key对应
/// </summary>
public enum PromotionPack
{
    None,
    pack_remove_ads,
    pack_limited_1,
    pack_limited_2,
    pack_limited_3,
    pack_limited_4,
    pack_limited_5,
    pack_limited_6,
    pack_limited_7,
    //锦标赛相关
    pack_tournament_1,
    pack_tournament_2,
    pack_tournament_3,
    pack_tournament_4,
    pack_tournament_5,
    pack_tournament_6,
    ////超级小猪
    piggy_bank_3,
}

public class DataIAP : MonoSingleton<DataIAP>
{
    #region 外部接口
    
    public CfgShop GetShopItem(string iapKey)
    {
        return ConfigManager.Instance.GetShopDataByIapKey(iapKey);
    }
    
    public void Purchase(string iap_key,Action<PurchaseResult, string> callBack)
    {
        //统计数据
        IsNeedSave = true;
        Action<PurchaseResult, string, PurchaseEventArgs> purchaseCallback =
            (PurchaseResult purchaseResult, string iapKey, PurchaseEventArgs purchaseEventArgs) =>
            {
                GameLogger.LogError("purchaseResult========" + purchaseResult);
                GameLogger.LogError("iapKey=========" + iapKey);
                if (purchaseResult == PurchaseResult.Success)
                {
                    CfgShop _cfgShop = GetShopItem(iapKey);
                    AdjustEvent adjustEvent = new AdjustEvent("e3j388");
                    adjustEvent.setRevenue(_cfgShop.Prices, "USD");
                    Adjust.trackEvent(adjustEvent);
                    
                }
                else
                {
                    /*MsgBoxEvent.ShowMsgBoxOkTitle showMsgBoxOkTitle = new MsgBoxEvent.ShowMsgBoxOkTitle();
                    showMsgBoxOkTitle.Title = Utils.GetTextByID("900091");
                    showMsgBoxOkTitle.Content = Utils.GetTextByID("900092");
                    showMsgBoxOkTitle.BtnName = Utils.GetTextByID("900129");
                    EventManager.Instance.SendMessage(showMsgBoxOkTitle);*/
                }
                
                if (callBack != null)
                {
                    callBack(purchaseResult, iapKey);
                }

                GameLogger.Log(MotionFramework.Console.GameLogType.IAP, $"购买流程结束!交易状态:{purchaseResult} - {iapKey}");
            };

        if (Application.isEditor)
        {
            _isFreeBuy = true;
        }
	
/*#if UNITY_ANDROID
        _isFreeBuy = true;
#endif*/
        
        if (_isFreeBuy)
        {
            purchaseCallback(PurchaseResult.Success, iap_key, null);
        }
        else
        {
            IAPMgr.Instance.Purchase(iap_key, purchaseCallback);
        }
    }
    
    #endregion

    #region GM后门

    /// <summary>
    /// 是否免费购买
    /// </summary>
    /// <param name="isFreeBuy"></param>
    public void FreeBuy(bool isFreeBuy)
    {
        _isFreeBuy = isFreeBuy;
    }

    #endregion
    
    #region 存档

    public string SaveKey => "DataIAPSaveKey";

    public bool IsNeedSave { get; set; }
    
    #endregion

    private bool _isFreeBuy = false;
    
    /// <summary>
    /// 检测是否在当前平台显示
    /// </summary>
    /// <param name="platform"></param>
    /// <returns></returns>
    private bool CheckPlatform(int platform)
    {
        IAPPlatform iapPlatform = (IAPPlatform)platform;
        if (iapPlatform == IAPPlatform.All)
        {
            return true;
        }

        if (Application.platform == RuntimePlatform.IPhonePlayer
            && iapPlatform == IAPPlatform.IOS)
        {
            return true;
        }
        else if (Application.platform == RuntimePlatform.Android
                 && (iapPlatform == IAPPlatform.Google || iapPlatform == IAPPlatform.Amazon))
        {
            return true;
        }

        return false;
    }
    
    /// <summary>
    /// 
    /// </summary>
    public enum NOADS
    {
        Always = 0, //一直显示
        Noads = 1, //带去广告
        Normal = 2, //普通
    }

    /// <summary>
    /// 
    /// </summary>
    public enum UserGarde
    {
        Free = 1,
        Low = 2,
        Mid = 3,
        High = 4,
        Special = 5,
    }

    enum IAPItemType
    {
        Pack = 1, //礼包
        Goods = 2, //单独商品
        PiggyBank = 3, //存钱罐
    }

    public enum IAPPlatform
    {
        All = 0, //全平台
        IOS = 1, //ios
        Google = 2,
        Amazon = 3,
    }

    public class IAPStatisticsData
    {
        /// <summary>
        /// 累计内购金额
        /// </summary>
        public float HistoryIAP;

        /// <summary>
        /// 上一次内购时的关卡
        /// </summary>
        public int LastIAPLevel = -1;

        /// <summary>
        /// 上一次内购时的活跃天数
        /// </summary>
        public int LastIAPActiveDay = 0;

        /// <summary>
        /// 单次iap最大金额
        /// </summary>
        public float MaxIAPPrice = 0f;

        /// <summary>
        /// 成功内购次数
        /// </summary>
        public int IapSuccessTimes;

        /// <summary>
        /// 打开商店次数
        /// </summary>
        public int OpenShopTimes = 0;

        /// <summary>
        /// 点击内购次数
        /// </summary>
        public int ClickIapTimes = 0;

        /// <summary>
        /// 最近两次iap 价格
        /// </summary>
        public double[] LastTwoIapPrice = new double[] { 0f, 0f };

        /// <summary>
        /// 玩家购买内购次数统计
        /// </summary>
        public Dictionary<string, int> PurchCountDic = new Dictionary<string, int>();
    }
}

enum ItemType
{
    Pack = 1, //礼包
    Goods = 2, //单独商品
    PiggyBank = 3, //存钱罐
}